package wwj.controller;

import wwj.bean.ShopCartData;
import wwj.service.ShopCartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/ShopCartServlet")
public class ShopCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 取数据
        String id = req.getParameter("id");
        String serviceType = req.getParameter("serviceType");
        HttpSession session = req.getSession();
        ShopCartData myCart = (ShopCartData)session.getAttribute("myCart");
        if (myCart == null){
            myCart = new ShopCartData();
            session.setAttribute("myCart",myCart);
        }

        ShopCartService.dispath(Integer.parseInt(serviceType),myCart,id);
//        ShopCartService.addToShopCart(myCart,id);
        resp.sendRedirect("shopCart.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
